<?php
namespace Home\Controller;
use Think\Model;
use Think\Controller;
class ContactController extends Controller {

    /**
     * 验证码
     */
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->useImgBg = false;  //不设置背景图片
        $Verify->imageW = 130;
        $Verify->imageH = 40;
        $Verify->length = 4;
        $Verify->entry();
    }

    public function check_verify($code,$reset = false, $id = '',$is_ajaxreturn = true){
        $verify = new \Think\Verify();
        $verify->reset = $reset;
        $res = $verify->check($code, $id);
        if(!$is_ajaxreturn){
            return $res;
        }
        if(IS_AJAX){
            header('Content-Type:application/json; charset=utf-8');
            $data = json_encode($res);
            exit($data);
        }else{
            return $res;
        }
    }

    /**
     * @param int $id
     */
    public function index($id = 0){
        if(IS_POST){
            if(!$this->check_verify(I('post.verify'),false,'',false)){
                $this->ajaxReturn(-1);
            }
            $rules = array(
                array('username', 'require', '姓名是必须的'),
                array('email', '/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/', '请填写正确的邮箱'),
                array('mobile', '/^(?:13|15|18|17|14)[0-9]{9}$/', '请填写正确的电话'),
                array('msg','require','请填写信息'),
            );
            $Leavemsg = M("Leavemsg"); // 实例化User对象
            if (!$Leavemsg->validate($rules)->create()) {
//                $this->error($Leavemsg->getError());
                $this->ajaxReturn(-2);
            } else {
                $data = I("post.");
                $data['c_time'] = time();
                if ($Leavemsg->data($data)->add()) {
                    $this->ajaxReturn(1);
                } else {
                    $this->ajaxReturn(-2);
                }
            }
        }else{
            $one = M('CompanyInfo')
                ->order('id desc')
                ->find();
            $this->assign('one',$one);
            $this->display();
        }
    }


}